/*
 * Copyright (c) 2009-2010 VSC co.,Ltd. All rights reserved.
 */
package org.googlecode.viewt.core.tools.po;

import java.util.HashMap;
import java.util.Map;

import org.googlecode.viewt.core.tools.po.types.PersistObjectDescDef;
import org.googlecode.viewt.core.tools.po.types.PersistObjects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author <mailto: lizhl@vsc.com>
 * 
 */
public class POConfigRepository {
	private static Logger logger = LoggerFactory.getLogger(POConfigRepository.class);
	private Map pomap = new HashMap();
	private PersistObjects mainDef = null;
	private String prePackName = null;
	private String mainDefFile = null;

	public POConfigRepository() {
	}

	public void setMainDef(String cfgFile, String prePackName) {
		this.mainDefFile = cfgFile;
		this.prePackName = prePackName;

		logger.debug(new StringBuffer().append(
				"Begin load main PO config file:").append(cfgFile).toString());

		this.mainDef = POConfigUtil.loadConfig(cfgFile, prePackName);

		if (mainDef == null) {
			logger.error(new StringBuffer().append(
					"load main definition fail, check your config file:")
					.append(cfgFile).toString());
			return;
		}

		addPOConfig(mainDef.getPersistObjectDescDef());
		logger.debug("Set main PO config done.");
	}

	public PersistObjects getMainDef() {
		return mainDef;
	}

	public String getMainDefFile() {
		return mainDefFile;
	}

	public String getPrePackName() {
		return prePackName;
	}

	public void addPOConfig(String cfgFile) {
		if (this.mainDefFile == null) {
			logger.error("main definition not set, set it first");
			return;
		}

		PersistObjects pos = POConfigUtil.loadConfig(cfgFile, this.prePackName);
		if (pos == null) {
			logger.error("load PO definition fail, check your config file:"
					+ cfgFile);
			return;
		}

		addPOConfig(pos.getPersistObjectDescDef());
	}

	public void addPOConfig(PersistObjectDescDef poDef) {
		if (poDef == null) {
			return;
		}
		String className = poDef.getClassName();
		if (!(this.pomap.containsKey(className)))
			this.pomap.put(className, poDef);
	}

	//
	public void addPOConfig(PersistObjectDescDef[] poDefs) {
		int len = (poDefs == null) ? 0 : poDefs.length;
		for (int i = 0; i < len; ++i)
			addPOConfig(poDefs[i]);
	}

	//
	public PersistObjectDescDef getPOConfig(String className) {
		PersistObjectDescDef poDef;
		String depCfgFile;
		PersistObjects pos;
		poDef = (PersistObjectDescDef) pomap.get(className);
		if (poDef == null) {
			depCfgFile = POConfigUtil.findDepCfgFile(mainDefFile, className);
			if (depCfgFile != null) {
				logger.debug(new StringBuffer().append(
						"retry dependent PO config file:").append(depCfgFile)
						.toString());
				pos = POConfigUtil.loadConfig(depCfgFile, prePackName);
				if (pos != null) {
					addPOConfig(pos.getPersistObjectDescDef());
					poDef = (PersistObjectDescDef) pomap.get(className);
				}
			}
		}
		return poDef;
	}

	
	public PersistObjectDescDef[] getCurPOConfig() {
		return getPOConfigByModule(getProjectName(), getModuleName());
	}

	public PersistObjectDescDef[] getPOConfigByModule(String projectName,
			String moduleName) {
		java.util.List confList;
		java.util.Iterator iterator;
		String className;
		confList = new java.util.ArrayList();
		iterator = pomap.keySet().iterator();
		while (iterator.hasNext()) {
			className = (String) iterator.next();
			if ((projectName.equals(POConfigUtil.getPOProject(className)))
					&& (moduleName.equals(POConfigUtil.getPOModule(className)))) {
				confList.add(pomap.get(className));
			}
		}
		return (PersistObjectDescDef[]) confList
				.toArray(new PersistObjectDescDef[0]);
	}

	//
	// public PersistObjectDescDef[] getEmbedPOByModule(String projectName,
	// String moduleName) {
	// java.util.List confList;
	// Iterator iterator;
	// String className;
	// PersistObjectDescDef poDef;
	// confList = new ArrayList();
	// iterator = pomap.keySet().iterator();
	// while (iterator.hasNext()) {
	// className = (String) iterator.next();
	// poDef = (PersistObjectDescDef) pomap.get(className);
	// if ((projectName.equals(POConfigUtil.getPOProject(className)))
	// && ((moduleName.equals(POConfigUtil.getPOModule(className))) && (null !=
	// poDef
	// .getEmbedObjectDef()))) {
	// confList.add(poDef);
	// }
	// }
	// return (PersistObjectDescDef[]) confList
	// .toArray(new PersistObjectDescDef[0]);
	// }
	//
	// public String[] poAncestorClass(String className) {
	// java.util.List ancestorList;
	// PersistObjectDescDef poDef;
	// String parentClass;
	// String[] ancestors;
	// int i;
	// ancestorList = new ArrayList();
	// poDef = getPOConfig(className);
	// while ((poDef != null) && (poDef.getParentClassName() != null)) {
	// parentClass = poDef.getParentClassName();
	// ancestorList.add(parentClass);
	// poDef = getPOConfig(parentClass);
	// }
	// ancestors = new String[ancestorList.size()];
	// i = 0;
	// while (i < ancestors.length) {
	// ancestors[i] = (String) ancestorList.get(i);
	// i++;
	// }
	// return ancestors;
	// }

	public String getProjectName() {
		return (mainDef == null) ? null : mainDef.getProjectName();
	}

	public String getModuleName() {
		return (mainDef == null) ? null : mainDef.getModuleName();
	}

}
